package com.seaboxdata.interfaceJob.queuing;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.queuing.DischargePass;
import com.seaboxdata.entity.queuing.DisposalPass;
import com.seaboxdata.service.queuing.BuildEngineerSmartMaginformationGetDischargePassServer;
import com.seaboxdata.service.queuing.BuildEngineerSmartMaginformationGetDisposalPassServer;
import com.seaboxdata.utils.SSLClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Component
@Configuration
@Slf4j
public class BuildEngineerSmartMagInformationJob {
    @Autowired
    private BuildEngineerSmartMaginformationGetDisposalPassServer buildEngineerSmartMaginformationGetDisposalPassServer;
    @Autowired
    private BuildEngineerSmartMaginformationGetDischargePassServer buildEngineerSmartMaginformationGetDischargePassServer;

    @Value("${BuildEngineerSmartMagInformation.getDischargePass.url}")
    private String DischargePassUrl;

    @Value("${BuildEngineerSmartMagInformation.getDisposalPass.url}")
    private String DisposalPassUrl;

    @Value("${interface.isTest}")
    private String isTest;

    public String token()   {
        String teken=null;
        // 定义请求的参数
        HttpClient client = null;
        HttpPost request;
        List<Object> results = new ArrayList<>();
        try {
            client = new SSLClient();
            request = new HttpPost(new URI("https://jzfqw.szsti.org/szcwm/api/out/getToken"));
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("accessKey", "DAPENG4A6A03C1FFC069A4F30FCA65D");
            jsonParam.put("secretKey"," ED02674B03D116F25DF2D2AAD44D2A65");
            String parm ="accessKey=DAPENG4A6A03C1FFC069A4F30FCA65D&secretKey=ED02674B03D116F25DF2D2AAD44D2A65";
            String parm1 ="{\"accessKey\": \"DAPENG4A6A03C1FFC069A4F30FCA65D\",\"secretKey\":\"ED02674B03D116F25DF2D2AAD44D2A65\" }";
            StringEntity stringEntity = new StringEntity(parm1);

            //stringEntity.setContentType("application/x-www-form-urlencoded");

            //StringEntity s = new StringEntity(jsonParam.toString(), "utf-8");
            stringEntity.setContentType("application/json");
            request.setEntity(stringEntity);
            request.setHeader("accessKey", "DAPENG4A6A03C1FFC069A4F30FCA65D");
            request.setHeader("secretKey", "ED02674B03D116F25DF2D2AAD44D2A65");


            HttpResponse response = client.execute(request);
            int returncode = response.getStatusLine().getStatusCode();
            if (returncode == 200) {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                JSONObject speeds = null;
                speeds = (JSONObject) JSONObject.parse(result);
                speeds= (JSONObject)speeds.get("Content");
                teken = (String) speeds.get("token");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return teken;
    }
    @Scheduled(cron = "${BuildEngineerSmartMagInformation.getDischargePass.Scheduled}")
    public void getDischargePass(){
        //如果是测试,直接退出
        if("true".equals(isTest)){
            return;
        }
        try {

            List<DischargePass> dischargePass=sendDischargePass(DischargePassUrl, token(),DischargePass.class);
            buildEngineerSmartMaginformationGetDischargePassServer.truncate();
            buildEngineerSmartMaginformationGetDischargePassServer.saveBatch(dischargePass);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List sendDischargePass(String url, String token,  Class<?> cls)  {
        HttpClient client = null;
        HttpPost request;
        List<Object> results = new ArrayList<>();
        try {
            client = new SSLClient();
            request = new HttpPost(new URI(url));
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("token", token);
            String parm1 ="{\"token\":\""+ token+"\" }";
            StringEntity stringEntity = new StringEntity(parm1);
            stringEntity.setContentType("application/json");
            request.setEntity(stringEntity);
            HttpResponse response = client.execute(request);
            int returncode = response.getStatusLine().getStatusCode();
            if (returncode == 200) {

                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                JSONObject speeds = null;
                speeds = (JSONObject) JSONObject.parse(result);
                JSONArray speeds1 = null;
                speeds1= (JSONArray)speeds.get("Content");
                Iterator<Object> iterator = speeds1.iterator();
                Date date=new Date();
                while (iterator.hasNext()) {
                    JSONObject next = (JSONObject) iterator.next();
                    next.put("cjsj",date);
                    Object t = next.toJavaObject(cls);
                    results.add(t);

                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return results;
    }
    @Scheduled(cron = "${BuildEngineerSmartMagInformation.getDisposalPass.Scheduled}")
    public void getDisposalPass(){
        //如果是测试,直接退出
        if("true".equals(isTest)){
            return;
        }
        try {
            List<DisposalPass> disposalPass=sendgetDisposalPass(DisposalPassUrl, token(), DisposalPass.class);
            buildEngineerSmartMaginformationGetDisposalPassServer.truncate();
            buildEngineerSmartMaginformationGetDisposalPassServer.saveBatch(disposalPass);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //@Scheduled(cron = "0 */1 * * * ?")
    public List sendgetDisposalPass(String url, String token,  Class<?> cls)  {
        HttpClient client = null;
        HttpPost request;
        List<Object> results = new ArrayList<>();
        try {
            client = new SSLClient();
            request = new HttpPost(new URI(url));
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("token", token);
            String parm1 ="{\"token\":\""+ token+"\" }";
            StringEntity stringEntity = new StringEntity(parm1);
            stringEntity.setContentType("application/json");
            request.setEntity(stringEntity);
            HttpResponse response = client.execute(request);
            int returncode = response.getStatusLine().getStatusCode();
            if (returncode == 200) {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                JSONObject speeds = null;
                speeds = (JSONObject) JSONObject.parse(result);
                JSONArray speeds1 = null;
                speeds1= (JSONArray)speeds.get("Content");
                Iterator<Object> iterator = speeds1.iterator();
                Date date=new Date();
                while (iterator.hasNext()) {
                    JSONObject next = (JSONObject) iterator.next();
                    next.put("cjsj",date);
                    Object t = next.toJavaObject(cls);
                    results.add(t);

                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return results;
    }
}
